﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Animations;
using Silvermoon.Controls.Base;

namespace Silvermoon.Controls.Classes
{
    /// <summary>
    /// Performs insert/remove animation for a ListViewItem.
    /// </summary>
    internal class ListViewItemAnimation : AnimEventArgs
    {
        public const int DefaultDuration = 350;

        private bool performRemove;
        private int itemIndex;
        private ListViewItem item;

        public ListViewItemAnimation(ListViewItem item)
            : base(DefaultDuration, 0, 255, 0)
        {
            if (item == null) throw new ArgumentNullException("item");
            this.item = item;
            this.itemIndex = item.ItemIndex;
            ActualHeight = item.Height;
        }

        public int ActualHeight { get; private set; }

        public bool IsSelected { get; private set; }

        public void PerformInsert()
        {
            Invoke();
        }

        public void PerformRemove()
        {
            performRemove = true;
            ListViewBase listView = item.ListView;
            IsSelected = listView.SelectedItem == item;

            item.Alpha = 255;
            Invoke();
        }

        protected override void OnProgress(int time)
        {
            item.Alpha = 255 - Value;
            base.OnProgress(time);
            int value = !performRemove ? Value : 255 - Value;
            ActualHeight = (value * item.Height) / 255;
            item.ListView.Invalidate(InvalidationFlag.LayoutAppearance);
        }


        protected override void OnComplete()
        {
            base.OnComplete();
         //   item.animation = null;
        }

    }
}
